package com.db;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper {
	private static String DB_PATH = "/data/data/com.tutorial/databases/";

	private static String DB_NAME = "LocationDB5";

	private SQLiteDatabase myDataBase;

	private final Context myContext;

	public DataBaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this.myContext = context;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	public void createDataBase() throws IOException {

		boolean dbExist = checkDataBase();

		if (dbExist) {
			Log.v("hh", "Co rui");

		} else {

			this.getReadableDatabase();

			try {

				copyDataBase();

			} catch (IOException e) {

				throw new Error("Error copying database");

			}
		}

	}

	/*public void createDataBase() throws IOException {

		boolean dbExist = checkDataBase();

		if (dbExist) {
			Log.v("hh", "Co rui");

		} else {

			myDataBase = this.getReadableDatabase();
			myDataBase.execSQL("Create table place(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, picture TEXT, address TEXT, location TEXT,des TEXT);");
			ContentValues cv = new ContentValues();
			cv.put("name", "Vịt cỏ Thành Luân");
			cv.put("address", "Số 9, ngõ 2, Phú yên, Bồ Đề - Long Biên - Hà Nội");
			cv.put("location", "0.0 0.0");
			cv.put("des", "Có dịp đi qua cầu Chương Dương chắc hẳn nhiều người sẽ ấn tượng với tấm biển hiệu Vịt nướng đi em của nhà hàng Vịt cỏ Thành Luân");
			SQLiteDatabase db = this.getWritableDatabase();
			myDataBase.insert("place", null, cv);
			
		
		}

	}
*/
	public boolean checkDataBase() {
		SQLiteDatabase db = null;
		try {
			String path = DB_PATH + DB_NAME;
			db = SQLiteDatabase.openDatabase(path, null,
					SQLiteDatabase.NO_LOCALIZED_COLLATORS);
		} catch (SQLiteException ex) {

		}
		if (db != null) {
			db.close();
		}
		return db != null ? true : false;
	}

	private void copyDataBase() throws IOException {
		InputStream myInput = myContext.getAssets().open(DB_NAME);

		// Path to the just created empty db
		String outFileName = DB_PATH + DB_NAME;

		// Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();

	}

	public void openDataBase() throws SQLException {
		String myPath = DB_PATH + DB_NAME;
		myDataBase = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.NO_LOCALIZED_COLLATORS);

	}

	@Override
	public synchronized void close() {

		if (myDataBase != null)
			myDataBase.close();

		super.close();

	}

	public List<String> getAllData() {
		List<String> a = new ArrayList<String>();
		myDataBase = this.getWritableDatabase();
		Cursor mCursor = myDataBase.rawQuery("Select * from place", null);
		mCursor.moveToFirst();
		while (!mCursor.isAfterLast()) {
			String s = "";
			s += mCursor.getString(1);
			s += " ";
			s += mCursor.getString(3);
			a.add(s);
			mCursor.moveToNext();

		}
		myDataBase.close();
		mCursor.close();
		close();
		return a;
	}

	/*public int countRecord() {
		myDataBase = this.getWritableDatabase();
		Cursor mCursor = myDataBase.rawQuery("Select * from " + "place", null);
		return mCursor.getCount();
	}*/

	public List<String> getOne(String table, int id) {
		String result = "";
		List<String> a = new ArrayList<String>();
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery("Select * from " + table + " where id= "
				+ id, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			for (int i = 1; i < 6; i++) {
				result = cursor.getString(i);
				a.add(result);
			}
			cursor.moveToNext();
		}
		db.close();
		cursor.close();
		close();
		return a;
	}

	public int upDateLocation(String value, int id) {
		ContentValues cv = new ContentValues();
		cv.put("location", value);
		String where = "id" + " = " + id;
		SQLiteDatabase db = this.getWritableDatabase();
		return db.update("place", cv, where, null);

	}

	public int insertLocation(String name, String add, String loc, String des) {
		ContentValues cv = new ContentValues();
		cv.put("name", name);
		cv.put("address", add);
		cv.put("location", loc);
		cv.put("des", des);
		SQLiteDatabase db = this.getWritableDatabase();
		return (int) db.insert("place", null, cv);
	}

	public int deleteLocation(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		return db.delete("place", "id= " + id, null);
	}

}
